package com.shambles.manage.serviceImpl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shambles.manage.mapper.PhotoMapper;
import com.shambles.manage.mapper.UsersMapper;
import com.shambles.manage.model.Photo;
import com.shambles.manage.model.Users;
import com.shambles.manage.model.dto.SubScribeDTO;
import com.shambles.manage.model.entity.BasePage;
import com.shambles.manage.model.entity.MessageRequestVM;
import com.shambles.manage.service.BoothService;
import com.shambles.manage2.tkmapper.entity.*;
import com.shambles.manage2.tkmapper.mapper.*;
import com.shambles.manage2.utils.CommonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BoothServiceImpl implements BoothService {

    @Resource
    private AbattoirBoothMapper abattoirBoothMapper;
    @Resource
    private SubscribeInformationMapper informationMapper;
    @Resource
    private SubscribeDetailMapper detailMapper;
    @Resource
    private CattleMapper cattleMapper;
    @Resource
    private UsersManagerMapper usersManagerMapper;
    @Resource
    private UsersinfoMapper usersinfoMapper;
    @Resource
    private PhotoMapper photoMapper;
    @Resource
    private CostMapper costMapper;
    @Resource
    private SysLogMapper sysLogMapper;
    @Resource
    private UsersMapper usersMapper;

    /**
     * 查询所有摊位并分页
     * @param messageRequestVM
     * @return
     */
    @Override
    public PageInfo<AbattoirBooth> findAllBooth(MessageRequestVM messageRequestVM) {
        List<AbattoirBooth> allBooth = abattoirBoothMapper.findAllBooth();
        return PageHelper.startPage(messageRequestVM.getPageIndex(), messageRequestVM.getPageSize()).doSelectPageInfo(() ->
                abattoirBoothMapper.findAllBooth()
        );
    }

    /**
     * 查询空置摊位并分页
     * @param messageRequestVM
     * @return
     */
    @Override
    public PageInfo<AbattoirBooth> findEmptyBooth(MessageRequestVM messageRequestVM) {
        return PageHelper.startPage(messageRequestVM.getPageIndex(), messageRequestVM.getPageSize()).doSelectPageInfo(() ->
                abattoirBoothMapper.findEmptyBooth()
        );
    }

    /**
     * 屠宰种类
     * @return
     */
    @Override
    public List<String> kind() {
        List<String> list = cattleMapper.kind();
        return list;
    }

    /**
     * 返回管理员的信息
     * @return
     */
    @Override
    public List<UsersManager> returnOpenID() {
        return usersManagerMapper.findManager();
    }

    /**
     * 根据openID返回信息
     * @param openId
     * @return
     */
    @Override
    public Usersinfo findByOpenID(String openId) {
        return usersinfoMapper.findByOpenID(openId);
    }

    /**
     * 预约人的查看自己的检疫证明
     * @param guid
     * @return
     */
    @Override
    public List<Photo> findSimple(String guid) {
        return photoMapper.findByGuid(guid);
    }

    /**
     * 审核预约发给管理后台
     * @param messageRequestVM
     * @return
     */
    @Override
    public PageInfo<SubscribeInformation> findCheckSubscribe(MessageRequestVM messageRequestVM) {
        return PageHelper.startPage(messageRequestVM.getPageIndex(), messageRequestVM.getPageSize()).doSelectPageInfo(() ->
                informationMapper.findCheckSubscribe(messageRequestVM.getStatus())
        );
    }

    @Override
    public PageInfo<SubscribeInformation> findAllSubscribe(MessageRequestVM messageRequestVM) {
        return PageHelper.startPage(messageRequestVM.getPageIndex(), messageRequestVM.getPageSize()).doSelectPageInfo(() ->
                informationMapper.findAllSubscribe()
        );
    }

    /**
     * 管理员拒绝或同意申请
     * @param id
     * @param i
     * @return
     */
    @Override
    public int deal(int id,int i) {
        SubscribeInformation information = new SubscribeInformation();
        information.setStatus(i);
        information.setId(id);
        int deal = informationMapper.deal(information);
        return deal;
    }

    /**
     * 根据id返回对象
     * @return
     */
    @Override
    public SubscribeInformation findOpenIdByID(Integer id) {
        return informationMapper.findOpenIdByID(id);
    }

    @Override
    public PageInfo<Cost> slaughter(MessageRequestVM messageRequestVM) {
        return PageHelper.startPage(messageRequestVM.getPageIndex(), messageRequestVM.getPageSize()).doSelectPageInfo(() ->
                costMapper.findAll(messageRequestVM.getName())
        );
    }


    @Override
    public PageInfo<Users> returnAllUsers(BasePage basePage) {
        List<Users> users = usersMapper.returnAllUsers();
        PageInfo<Users> objectPageInfo = PageHelper.startPage(basePage.getPageIndex(), basePage.getPageSize()).doSelectPageInfo(() ->
                usersMapper.returnAllUsers()
        );
        List<Users> list = objectPageInfo.getList();
        return objectPageInfo;
    }

    /**
     * 添加操作人到当条数据
     * @param id
     * @param currentUserName
     */
    @Override
    public void updateById(Integer id, String currentUserName) {
        SubscribeInformation information = new SubscribeInformation();
        information.setId(id);
        information.setPersonnelName(currentUserName);
        informationMapper.updateById(information);
    }




    /**
     * 查询预约表是否时间有冲突
     * @param subScribeDTO
     * @return
     */
    @Override
    public AbattoirBooth findSubscribe(SubScribeDTO subScribeDTO) {
        AbattoirBooth abattoirBooth = informationMapper.isConflict(subScribeDTO);
        return abattoirBooth;
    }

    /**
     * 预约信息写入预约表
     * @param information
     * @return
     */
    @Override
    public int insertSub(SubscribeInformation information, List<SubscribeDetail> list,String name) {
        int insert = informationMapper.insertSelective(information);
        for (SubscribeDetail subscribeDetail : list) {
            int insert1 = detailMapper.insertSelective(subscribeDetail);
        }
            //添加系统日志
            SysLog sysLog=new SysLog();
            sysLog.setGuid(IdUtil.simpleUUID());
            sysLog.setOperationPeople(name);    //预约人姓名
            sysLog.setFunctionName("预约");
            sysLog.setOperationTime(System.currentTimeMillis());
            sysLog.setContent(name+"操作预约功能");
        this.sysLogMapper.insertSelective(sysLog);
        return insert;
    }
}
